{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bayes Decision Rule\n",
    "*Complete and hand in this completed worksheet (including its outputs and any supporting code outside of the worksheet) with your assignment submission. Please check the pdf file for more details.*\n",
    "\n",
    "In this exercise you will:\n",
    "    \n",
    "- implement the calculation of **likelihood** of each features given particular class \n",
    "- implement the calculation of **posterior** of each class given particular feature\n",
    "- implement the calculation of **minimal total risk** of bayes decision rule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# some basic imports\n",
    "import scipy.io as sio\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = sio.loadmat('data.mat')\n",
    "x1_train, x1_test, x2_train, x2_test = data['x1_train'], data['x1_test'], data['x2_train'], data['x2_test']\n",
    "all_x = np.concatenate([x1_train, x1_test, x2_train, x2_test], 1)\n",
    "data_range = [np.min(all_x), np.max(all_x)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from get_x_distribution import get_x_distribution\n",
    "\n",
    "train_x = get_x_distribution(x1_train, x2_train, data_range)\n",
    "test_x = get_x_distribution(x1_test, x2_test, data_range)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 1 likelihood\n",
    "Your code for this section will be written inside **bayes_decision_rule/likehood.py**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAEKCAYAAADn+anLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpY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Vc34fp4Bf6V7l9BZg34GulBF4Nw1dTeOwP3v0/gxeAvxZnza3A+cleXm32/m87rpWDP1zPowz8C+GL+APgW8D99H5QTq5u/7vAJt72l1A54qYh+h0AbVZ4y46faXbul83zqyRztVF27tfO9qssd++Aa7p/rADLAH+pPv/+F/AqSN4n/8hna6D+3r24wXAZcBl3Tbru/tuO50Th39/BHX2fR9n1Bk6N/56qPuzO9l2nd06/jadX/ov61k38v1JJ7QeB56jc3RwKZ1zYH8BPNj99xXdtpPA7/d873u7P6e7gH/Tco1D/Zw7LYckqZHdTZKkRoaEJKmRISFJamRISJIaGRKSpEaGhCSpkSEhSWpkSEgDluQXuhM/LumOdN2R5MxR1yUdCQfTSUOQ5EN0Roz/LWC6qq4dcUnSETEkpCHozke1BXiWzrQSPxlxSdIRsbtJGo5XACfQuZvdkhHXIh0xjySkIUgyRedueyvpTP64fsQlSUfE+0lIA5bkV4D9VfWZJIuA/5nk7VX19VHXJs2XRxKSpEaek5AkNTIkJEmNDAlJUiNDQpLUyJCQJDUyJCRJjQwJSVKj/wcPE3j1iySAhwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from likelihood import likelihood\n",
    "\n",
    "l = likelihood(train_x)\n",
    "width = 0.35\n",
    "p1 = plt.bar(np.arange(data_range[0], data_range[1] + 1), l.T[:,0], width)\n",
    "p2 = plt.bar(np.arange(data_range[0], data_range[1] + 1) + width, l.T[:,1], width)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('$P(x|\\omega)$')\n",
    "plt.legend((p1[0], p2[0]), ('$\\omega_1$', '$\\omega_2$'))\n",
    "plt.axis([data_range[0] - 1, data_range[1] + 1, 0, 0.5])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "151\n"
     ]
    }
   ],
   "source": [
    "#TODO\n",
    "#compute the number of all the misclassified x using maximum likelihood decision rule\n",
    "\n",
    "# begin answer\n",
    "correct_class = [1 if a > 0 else 0 for a in l[0] - l[1]]\n",
    "\n",
    "train_set = np.concatenate([x1_train, x2_train], 1)\n",
    "train_index_bias = np.min(train_set)\n",
    "\n",
    "misclassified_number = 0\n",
    "\n",
    "for x in x1_test[0]:\n",
    "    if correct_class[x + train_index_bias] == 0:\n",
    "        misclassified_number = misclassified_number + 1\n",
    "\n",
    "for x in x2_test[0]:\n",
    "    if correct_class[x + train_index_bias] == 1:\n",
    "        misclassified_number = misclassified_number + 1\n",
    "        \n",
    "print(misclassified_number)\n",
    "# end answer\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2 posterior\n",
    "Your code for this section will be written inside **bayes_decision_rule/posterior.py**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAEKCAYAAADn+anLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAFUtJREFUeJzt3X+wXOVdx/H3tyF4FZC2SdoiNzGhRivSUuiVolWL0mISnQRnGAVaizYlw9iITq1jmCjN0FZoO/6gLT+MgJRqoWgR75RUoLadOlYwoSQpJEUuEWQNmjSlabFNIeXrH3uCm5t97t172T27N7xfM3ey55xnz/nm7N188pwfz4nMRJKkdl7U7wIkSYPLkJAkFRkSkqQiQ0KSVGRISJKKDAlJUlFtIRERN0TEroh4oLD8LRGxtfr5UkScXFdtkqT26uxJ3AgsmWD5fwBvzMzXAO8F1tdRlCSp7Ii6NpSZX4yIhRMs/1LL5D3AcK9rkiRNrLaQmKKVwGdK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+kUHGAL+tvp7/BtwQh8+55+heehga8t+XAZcBFxUtVld7bstNE8a/nQf6mz7OY6rM2g++OuR6nd3pO46qzp+gOY/+se2zOv7/qQZWk8Az9DsHaykeQ7sn4CHqz9fWrUdAa5ree/bq9/TMeA3+1Bnz77rDsshSSrycJMkqciQkCQVGRKSpCJDQpJUZEhIkooMCUlSkSEhSSoyJKQui4ifrAZ+HKrucn0wIk7qd13SdHgzndQDEfE+mneMfz/QyMzL+1ySNC2GhNQD1XhUG4F9NIeV+F6fS5KmxcNNUm+8FDia5tPshvpcizRt9iSkHoiIUZpP21tEc/DH1X0uSZoWnychdVlEvA3Yn5mfiIhZwJci4hcy83P9rk2aKnsSkqQiz0lIkooMCUlSkSEhSSoyJCRJRYaEJKnIkJAkFRkSkqSi/wMJdsR/ByjZdgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from posterior import posterior\n",
    "\n",
    "p = posterior(train_x)\n",
    "width = 0.35\n",
    "p1 = plt.bar(np.arange(data_range[0], data_range[1] + 1), p.T[:,0], width)\n",
    "p2 = plt.bar(np.arange(data_range[0], data_range[1] + 1) + width, p.T[:,1], width)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('$P(\\omega|x)$')\n",
    "plt.legend((p1[0], p2[0]), ('$\\omega_1$', '$\\omega_2$'))\n",
    "plt.axis([data_range[0] - 1, data_range[1] + 1, 0, 1.2])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "141\n"
     ]
    }
   ],
   "source": [
    "#TODO\n",
    "#compute the number of all the misclassified x using optimal bayes decision rule\n",
    "\n",
    "# begin answer\n",
    "correct_class = [1 if a > 0 else 0 for a in p[0] - p[1]]\n",
    "\n",
    "misclassified_number = 0\n",
    "\n",
    "for x in x1_test[0]:\n",
    "    if correct_class[x + train_index_bias] == 0:\n",
    "        misclassified_number = misclassified_number + 1\n",
    "\n",
    "for x in x2_test[0]:\n",
    "    if correct_class[x + train_index_bias] == 1:\n",
    "        misclassified_number = misclassified_number + 1\n",
    "        \n",
    "print(misclassified_number)\n",
    "# end answer\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 3 risk\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "risk = np.array([[0, 1], [2, 0]])\n",
    "#TODO\n",
    "#get the minimal risk using optimal bayes decision rule and risk weights\n",
    "\n",
    "# begin answer\n",
    "action_number, _ = risk.shape\n",
    "C, N = train_x.shape\n",
    "r = np.zeros((N,action_number))\n",
    "\n",
    "for i in range(N):\n",
    "    for j in range(action_number):\n",
    "        for k in range(C):\n",
    "            r[i,j] = r[i,j] + risk[j,k] * p[k,i]\n",
    "\n",
    "R = r.sum(axis=1)\n",
    "print(R.min())\n",
    "# end answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
